package com.han.jdbc.mybatis.generate;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import com.han.jdbc.mybatis._04.mappers.EmpMapper;
import com.han.jdbc.mybatis.bean.Emp;
import com.han.jdbc.mybatis.generate.mapper.SysSimcardMapper;
import com.han.jdbc.mybatis.generate.pojo.SysSimcard;

/*import com.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.dao.EmployeeMapper;*/

public class MyBatisTest {


	@Test
	public void testMbg() throws Exception { 
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		InputStream is = MyBatisTest.class.getClassLoader().getResourceAsStream("06-mbg.xml");
		
		/*File configFile = new File("06-mbg.xml");*/
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(is);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
				callback, warnings);
		myBatisGenerator.generate(null);
	}
	
	@Test
	public void testUpdate() throws Exception { 
		String resource="06mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			SysSimcardMapper mapper = sqlSession.getMapper(SysSimcardMapper.class);
			SysSimcard simcard = mapper.selectByPrimaryKey(1);
			System.out.println(simcard);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	public static void main(String[] args) {
		String resource="06mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			System.out.println(is.available());
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			System.out.println("fsd");
			sqlSession = sqlSessionFactory.openSession();
			SysSimcardMapper mapper = sqlSession.getMapper(SysSimcardMapper.class);
			SysSimcard simcard = mapper.selectByPrimaryKey(1);
			System.out.println(simcard);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	

}
